package cn.itcast.controller;

import cn.itcast.domain.Area;
import cn.itcast.service.AreaService;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.awt.print.Book;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author： ~Mr·Weng~
 * @CreateTime： 2019年-01月-23日  14:51
 * Look at the wine in the glass toast the rich first.
 */
@Controller
public class AreaController {

    @Autowired
    private AreaService areaService;

    @RequestMapping("/area_batchImport")
    @ResponseBody
    public String areaBatchImport(MultipartFile file){

        ArrayList<Area> areaList = new ArrayList<>();

        try {
            //1加载file对象
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(file.getInputStream());
            //2读取页
            HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
            //3读取页的行
            for (Row row : sheet) {
                //如果是第一行就跳过(标题)
                if (row.getRowNum()==0){
                    //没有行号就跳过
                    continue;
                }
                // 跳过空行
                if (row.getCell(0) == null|| StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
                    continue;
                }

                Area area = new Area();
                area.setId(row.getCell(0).getStringCellValue());
                area.setProvince(row.getCell(1).getStringCellValue());
                area.setCity(row.getCell(2).getStringCellValue());
                area.setDistrict(row.getCell(3).getStringCellValue());
                area.setPostcode(row.getCell(4).getStringCellValue());
                areaList.add(area);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        areaService.saveAll(areaList);
        return "ok";
    }

    @RequestMapping("/area_pageQuery")
    @ResponseBody
    private Map area_pageQuery(Area area ,int page , int rows){

        Page<Area> areaResult = areaService.findAll(area,page,rows);
        //获取总条数和每页内容
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",areaResult.getTotalElements());
        map.put("rows",areaResult.getContent());

        return map;
    }


    @RequestMapping("/area_save")
    public String area_save(Area area){

        areaService.save(area);
        return "redirect:/pages/base/area.html";

    }

    @RequestMapping("/area_findAll")
    @ResponseBody
    public List<Area> area_finAll(){
        List<Area> areaList = areaService.area_findAll();
        return areaList;
    }

    @RequestMapping("/area_export")
    @ResponseBody
    public String area_export(){
        try {
            areaService.findAllExport();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "ok";
    }

}
